package com.kunbo.cn.service.impl.core.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.customer.CustomerManageDao;
import com.kunbo.cn.entity.core.customer.CustomerManage;
import com.kunbo.cn.service.core.customer.CustomerManageService;
import com.kunbo.cn.util.encrypt.MD5Utils;

@Service
public class CustomerManageImpl implements CustomerManageService {

	@Autowired
	private CustomerManageDao dao;

	@Autowired
	private JdbcTemplate jdbc;


	@Override
	public Boolean delCustomer(String id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		CustomerManage customer = dao.findOne(id);
		if (customer != null)
			return true;
		else
			return false;
	}

	@Override
	public Boolean updPassword(String id, String password) {
		// TODO Auto-generated method stub
		CustomerManage customer = dao.findOne(id);
		customer.setLogin_password(MD5Utils.md5(password));
		customer = dao.saveAndFlush(customer);
		if (customer != null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String, Object>> selCustomerList(String code, String customerName, String loginName, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		String sql = "SELECT a.id ,a.customer_code,	a.customer_name,a.customer,	a.login_name,		a.login_password,	a.phone,	a.affiliated_organization,	a.tives,	a.type,	a.login_count,	a.last_login_time,	a.explains FROM	business_customer_users_manage AS a where 1= 1";
		if(loginName != null && !loginName.equals("")) {
			sql +=" and a.login_name like '%" +loginName+ "%'";
		}
		if(code != null && !code.equals("")) {
		sql += " and a.customer_code like '%" + code + "%'";
		}
		if(customerName != null && !customerName.equals("")) {
		sql += " and a.customer_name like '%" + customerName + "%'";
		}
		sql += " order by a.id desc limit " + (page*pageSize) + "," + pageSize;
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			map.put("count", list.size());
			String l = "select name from business_customer_manage where id = " + Integer.parseInt(map.get("customer").toString());
			List<Map<String, Object>> supplier =jdbc.queryForList(l);
			if(supplier != null) {
					map.put("customer", supplier.get(0).get("name").toString());
			}
		}
		
		return list;
	}
	


	@Override
	public Boolean updCustomer(String id, String code, Integer customer, String loginName, String customerName,
			String loginPassword, String phone, String organization, String type, Integer effective, String explain) {
		// TODO Auto-generated method stub
		CustomerManage customers  = dao.findOne(id);
		customers.setCustomer_code(code);
		customers.setCustomer(customer);
		customers.setLogin_name(loginName);
		customers.setCustomer_name(customerName);
		customers.setLogin_password(MD5Utils.md5(loginPassword));
		customers.setPhone(phone);
		customers.setAffiliated_organization(organization);
		customers.setType(type);
		customers.setTives(effective);
		customers.setExplains(explain);
		
		customers = dao.saveAndFlush(customers);
		if (customers != null)
			return true;
		else
			return false;
	}

	@Override
	public CustomerManage saveCustomer(String code, Integer customer, String loginName, String customerName,
			String loginPassword, String phone, String organization, String type, Integer effective, String explain) {
		// TODO Auto-generated method stub
		String customerSql = "select login_name from business_customer_users_manage where login_name = '" + loginName + "'";
		String supplierSql = "select login_name from business_supplier_users_manage where login_name = '" + loginName + "'";
		String transportSql = "select login_id from transport_unit_management where login_id = '" + loginName + "'";
		List<Map<String,Object>> customerList = jdbc.queryForList(customerSql);
		List<Map<String,Object>> supplierList = jdbc.queryForList(supplierSql);
		List<Map<String,Object>> transportList = jdbc.queryForList(transportSql);	
		if(customerList.size() == 0 && supplierList.size() == 0 && transportList.size() == 0) {
			CustomerManage customers  = new CustomerManage();
			customers.setCustomer_code(code);
			customers.setCustomer(customer);
			customers.setLogin_name(loginName);
			customers.setCustomer_name(customerName);
			customers.setLogin_password(MD5Utils.md5(loginPassword));
			customers.setPhone(phone);
			customers.setAffiliated_organization(organization);
			customers.setType(type);
			customers.setTives(effective);
			customers.setExplains(explain);
			customers = dao.saveAndFlush(customers);
			if (customers != null)
				return customers;
			else
				return null;
			
		}else {
			return null;
		}
	}

	@Override
	public List<Map<String,Object>> getById(String id) {
		// TODO Auto-generated method stub
		String sql = "SELECT a.id ,a.customer_code,	a.customer_name,a.customer,	a.login_name,		a.login_password,	a.phone,	a.affiliated_organization,	a.tives,	a.type,	b.name,	a.login_count,	a.last_login_time,	a.explains FROM	business_customer_users_manage AS a,business_customer_manage AS b WHERE	1 = 1 AND a.customer = b.id and a.id = '" + id + "'";
		return jdbc.queryForList(sql);
	}


	

}
